
defineClass('ViewController' ,{


             handleBtn: function(sender) {
             var tableViewCtrl = JPTableViewController.alloc().init()
             self.navigationController().pushViewController_animated(tableViewCtrl, YES)
             
            
       }
     },
     /**
      * 修改类方法，注意括号位置一一对应
      */
       {

            request: function(callback) {
                callback("message",ture)
            },
        
           
            
})
/** 列表 */
defineClass('JPTableViewController : UITableViewController <UIAlertViewDelegate>', ['data'], {

            viewDidLoad:function(){
              self.navigationItem().setTitle("第三页");
            } ,
            
            dataSource: function() {
            var data = self.data();
            if (data) return data;
            var data = [];
            for (var i = 0; i < 20; i ++) {
            data.push("cell from js " + i);
               }
            self.setData(data)
            return data;
      },
            numberOfSectionsInTableView: function(tableView) {
            return 1;
      },
            tableView_numberOfRowsInSection: function(tableView, section) {
            return self.dataSource().length;
      },
            tableView_cellForRowAtIndexPath: function(tableView, indexPath) {
            var cell = tableView.dequeueReusableCellWithIdentifier("cell")
            if (!cell) {
            cell = require('UITableViewCell').alloc().initWithStyle_reuseIdentifier(0, "cell")
      }
            cell.textLabel().setText(self.dataSource()[indexPath.row()])
            return cell
      },
            tableView_heightForRowAtIndexPath: function(tableView, indexPath) {
            return 60
      },
            tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {
            var alertView = require('UIAlertView').alloc().initWithTitle_message_delegate_cancelButtonTitle_otherButtonTitles("提示",self.dataSource()[indexPath.row()], self, "取消","确定",  null);
            alertView.show()
           
             // console.log("列表索引："+self.dataSource()[indexPath.row()]);
             // console.log("行数 ：" +indexPath.row());
      },     
            alertView_willDismissWithButtonIndex: function(alertView, idx) {
            // console.log('click btn ' + alertView.buttonTitleAtIndex(idx).toJS())
            if (idx==0) {
              console.log("取消按钮");
            } else {
             console.log("确认"); 
             var secondVC = SecondViewController.alloc().init()
             self.navigationController().pushViewController_animated(secondVC,YES)
            }
      }
            
})
defineClass('SecondViewController : UIViewController',{
      viewDidLoad:function(){
         self.super().viewDidLoad();
         self.navigationItem().setTitle("第三页");

         var width = require('UIScreen').mainScreen().bounds().width
         var btn = require('UIButton').alloc().initWithFrame({x:0, y:100, width:width, height:50})
         btn.setTitle_forState('这个按钮是通过JSPatch动态添加上去的啊', 0)
         btn.addTarget_action_forControlEvents(self, "handleBtn:", 1 << 6)
         btn.setBackgroundColor(require('UIColor').grayColor())
         self.view().addSubview(btn)
       
         var view = require('UILabel').alloc().initWithFrame({x:0,y:170,width:width/2,height:50})
         view.setBackgroundColor(require('UIColor').redColor())
         self.view().addSubview(view)
         view.setText("接口操作")
         view.setTextAlignment(1);
         view.setFrame({x:0,y:170,width:width,height:50});//重置宽高

         var secBtn = require('UIButton').alloc().initWithFrame({x:width/4, y:250, width:width/2, height:50})
         secBtn.setTitle_forState('同步GET请求接口', 0)
         secBtn.addTarget_action_forControlEvents(self, "secBtnClick:", 1 << 6)
         secBtn.setBackgroundColor(require('UIColor').grayColor())
         self.view().addSubview(secBtn)

         var thirdBtn = require('UIButton').alloc().initWithFrame({x:width/4, y:320, width:width/2, height:50})
         thirdBtn.setTitle_forState('异步GET请求接口', 0)
         thirdBtn.addTarget_action_forControlEvents(self, "thirdBtnClick:", 1 << 6)
         thirdBtn.setBackgroundColor(require('UIColor').grayColor())
         self.view().addSubview(thirdBtn)

         var fourBtn = require('UIButton').alloc().initWithFrame({x:width/4, y:390, width:width/2, height:50})
         fourBtn.setTitle_forState('AFN调用接口', 0)
         fourBtn.addTarget_action_forControlEvents(self, "fourBtnClick:", 1 << 6)
         fourBtn.setBackgroundColor(require('UIColor').grayColor())
         self.view().addSubview(fourBtn)

         var webBtn = require('UIButton').alloc().initWithFrame({x:width/4, y:460, width:width/2, height:50})
         webBtn.setTitle_forState('web网页操作', 0)
         webBtn.addTarget_action_forControlEvents(self, "webBtnClick:", 1 << 6)
         webBtn.setBackgroundColor(require('UIColor').grayColor())
         self.view().addSubview(webBtn)

      },

       handleBtn:function(sender){
         // console.log("点击按钮");
         var thirdVC = ThirdViewController.alloc().init();
         self.navigationController().pushViewController_animated(thirdVC,YES);
       },
       secBtnClick:function (sender) {
          console.log("请求接口操作");
           var url = require('NSURL').URLWithString(require('NSString').stringWithFormat('http://www.weather.com.cn/data/cityinfo/101010100.html'))
           var request = require('NSURLRequest').alloc().initWithURL_cachePolicy_timeoutInterval(url,0,1)
           var reciveData = require('NSURLConnection').sendSynchronousRequest_returningResponse_error(request,null,null)
           var str = require('NSString').alloc().initWithData_encoding(reciveData,4)
           console.log(str)

   },
       thirdBtnClick:function (sender) {
         // console.log("异步GET请求接口");
         var url = require('NSURL').URLWithString(require('NSString').stringWithFormat('http://www.weather.com.cn/data/cityinfo/101010100.html'))
         var request = require('NSURLRequest').alloc().initWithURL_cachePolicy_timeoutInterval(url,0,1)
         var connection = require('NSURLConnection').alloc().initWithRequest_delegate(request,self)

       },
       fourBtnClick:function (sender) {
         console.log("AFN调用接口")
         var urlString = require('NSString').stringWithFormat('http://url.cn/44iZbjV')
         require('AFHTTPSessionManager,AFNetworking,NSProgress,NSURLSessionDataTask,NSError,AFHTTPResponseSerializer,NSString')
         var mgr = AFHTTPSessionManager.manager()
         mgr.setResponseSerializer(AFHTTPResponseSerializer.serializer())
         mgr.GET_parameters_progress_success_failure(urlString, null, block('NSProgress*', function(downloadProgress) {

        }), block("NSURLSessionDataTask*, id", function(task, responseObject) {
            console.log("success = %@", NSString.alloc().initWithData_encoding(responseObject,4))
        }), block("NSURLSessionDataTask*, NSError*", function(task, error) {
            console.log("failure = %@", error)
        }));
       },
       webBtnClick:function (sender) {
         var webView = WebViewController.alloc().init()
         self.navigationController().pushViewController_animated(webView,YES)
       }
  
            })
defineClass('ThirdViewController:UIViewController',['data', 'totalCount'], {
            init: function () {
              self = self.super().init()
              self.setData(["a","b"])
              self.setTotalCount(2)
              return self
            },
            viewDidLoad:function() {
            self.super().viewDidLoad()
            self.navigationItem().setTitle("第四页")
            var str = "I'm JS String";
            var strs= new Array(); //定义一数组
            strs = require('NSString').stringWithFormat(str).componentsSeparatedByString(" ");
            console.log(strs);
            /**
             * 新增属性
             */
            var data = self.data()
            var totalCount = self.totalCount()
            
            },
            
            })

defineClass('WebViewController : UIViewController',{
            viewDidLoad: function () {
            self.super().viewDidLoad()
            self.navigationItem().setTitle("Web测试")
            // var slf = self;
            var weakSelf = __weak(self)
            return require('UIWebView').alloc().performSelectorInOC('initWithFrame:', [self.view().bounds()],function(webView){
                var url = require('NSURL').alloc().initWithString("https://www.baidu.com")
                webView.loadRequest(require('NSURLRequest').requestWithURL(url))
                weakSelf.view().addSubview(webView)
            });

 
   }
  
 })


















/*文本末尾*/
